<template>
  <div class="domain-set gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('sysManagement.domainConfiguration') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="180px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
      @keyup.enter="onSubmit(dataFormRef)"
    >
      <el-form-item
        :label="$t('sysManagement.apiDomain')"
        style="width:640px"
        prop="apiDomainName"
      >
        <el-input
          v-model="dataForm.apiDomainName"
          :placeholder="$t('sysManagement.apiDomainPH')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.shopSideDomain')"
        style="width:640px"
        prop="adminDomainName"
      >
        <el-input
          v-model="dataForm.adminDomainName"
          :placeholder="$t('sysManagement.shopSideDomainPH')"
          controls-position="right"
        />
      </el-form-item>
    </el-form>

    <div>
      <div class="dialog-footer">
        <div
          class="default-btn"
          @click="onSubmit(dataFormRef)"
        >
          {{ $t('sysManagement.save') }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const emit = defineEmits(['refreshDataList'])

const dataFormRef = ref(null)
const visible = ref(false)
const dataForm = ref({
  accessKeyId: null,
  accessKeySecret: null,
  signName: null
})
const dataRule = {
  apiDomainName: [
    { required: true, message: $t('sysManagement.apiDomainNullTips'), trigger: 'blur' }
  ],
  adminDomainName: [
    { required: true, message: $t('sysManagement.shopSideDomainNullTips'), trigger: 'blur' }
  ]
}

onMounted(() => {
  init()
})

const init = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/sys/pconfig/info/' + 'DOMAIN_CONFIG'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data) {
        dataForm.value = JSON.parse(data)
      }
    })
    dataFormRef.value?.resetFields()
  })
}
// 表单提交
const onSubmit = async formEl => {
  if (!formEl) return
  await formEl.validate(valid => {
    if (!valid) return
    http({
      url: http.adornUrl('/sys/pconfig/save'),
      method: 'post',
      data: http.adornData({
        paramKey: 'DOMAIN_CONFIG',
        paramValue: JSON.stringify(dataForm.value),
        remark: $t('sysManagement.domainConfig')
      })
    }).then(({ data }) => {
      if (data) {
        init()
      }
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}
</script>

<style lang="scss" scoped>
@use '../index.scss';
</style>
